/**
 * 绘制图层
 * @param {*} name 名称
 * @param {*} level 元素级别
 * @param {*} zIndex 图层级别
 */
function sPaintLayer(name, level, zIndex){
    var _name = name;
    var _level = level;
    var _zIndex = zIndex;

    var shapes = [];

    /**
     * 推入形状
     * @param {*} shape 形状
     */
    this.push = function(shape){
        shapes.push(shape);
    }

    /**
     * 遍历所有形状
     * @param {*} callback 回调函数
     */
    this.forEach = function(callback){
        paintShape.call(this, shapes, shapes.length, callback);
    }

    /**
     * 绘制形状
     * @param {*} shapeList 
     * @param {*} length
     * @param {*} callback 
     * @param {*} opt
     */
    var paintShape = function(shapeList, length, callback, opt){
        for(var i = 0; i < shapeList.length; i++){
            callback(shapeList[i], _level, opt);
            
            //表示在执行期间，又加入新的绘制形状
            if(length < shapes.length){
                let delShapes = shapes.splice(length, shapes.length - length);
                paintShape.call(this, delShapes, length, callback);
            }

            if(shapeList[i].afterMethodInvoke){
                shapeList[i].afterMethodInvoke(...shapeList[i].arguments);
            }
        }
    }

    /**
     * 定义name属性
     */
    Object.defineProperty(this, "name", {
        get: function(){
            return _name;
        }
    });
}

/**
 * 图层管理
 */
function sPaintLayerManager(){
    var layers = [];
    var maxZindex = 0;
    var minZindex = 0;

    /**
     * 获取当前最大图层等级
     * @returns 
     */
    this.getMaxZindex = function(){
        return maxZindex;
    }

    /**
     * 获取当前最小图层级别
     * @returns 
     */
    this.getMinZindex = function(){
        return minZindex;
    }

    /**
     * 创建图层
     * @param {*} name 图层名称
     * @param {*} level 元素层级
     * @param {*} handle 句柄
     * @param {*} zIndex 图层等级，不填默认是ssDict.sPaintLayerZindex.INLINE
     */
    this.createLayer = function(name, level, handle, zIndex){
        maxZindex = maxZindex < zIndex ? zIndex : maxZindex;
        minZindex = minZindex > zIndex ? zIndex : minZindex;

        minZindex = minZindex < 0 ? 0 : minZindex;

        if(!layers[handle]){
            layers[handle] = [];
        }

        zIndex = zIndex == null ? 100 : zIndex;

        if(!layers[handle][zIndex]){
            layers[handle][zIndex] = [];

            for(var i in ssDict.sPaintLayerLevel){
                layers[handle][zIndex][ssDict.sPaintLayerLevel[i]] = null;
            }
        }

        if(!layers[handle][zIndex][level]){
            layers[handle][zIndex][level] = new sPaintLayer(name, level, zIndex);
        }

        return layers[handle][zIndex][level];
    }

    /**
     * 遍历所有图层
     * @param {*} callback 回调函数
     */
    this.forEach = function(callback, handle){
        if(handle){
            let layer = null;
            for(var i in layers[handle]){
                layer = layers[handle][i];
                if(!layer) continue;
                
                for(var j in layer){
                    if(!layer[j]) continue;

                    layer[j].forEach(callback);
                }
            }
        }
    }

    /**
     * 获取所有图层
     */
    this.getLayers = function(){
        return layers;
    }

    /**
     * 清除图层内容
     * @param {*} level 元素层级
     * @param {*} handle 句柄
     * @returns 
     */
    this.clear = function(level, handle){
        var flg = false;

        if(level != null && handle != null){
            if(!layers[handle]) return flg;

            for(var i in layers[handle]){
                if(layers[handle][i] && layers[handle][i][level]){
                    layers[handle][i][level] = null;
                    flg = true;
                }
            }
        }else if(level != null){
            for(var i in layers){
                for(var j in layers[i]){
                    if(layers[i][j] && layers[i][j][level]){
                        layers[i][j][level] = null;
                        flg = true;
                    }
                }
            }
        }else if(level == null){
            for(var i in layers){
                layers[i] = null;
                flg = true;
            }
        }else if(layers){
            layers = null;
            flg = true;
        }

        return flg;
    }

    /**
     * 根据层级获取所在的所有句柄
     * @param {*} level 元素层级
     * @returns 
     */
    this.getHandleByLevel = function(level){
        var handles = [];
        for(var i in layers){
            for(var j in layers[i]){
                if(layers[i][j][level]){
                    handles.push(parseInt(i));
                }
            }
        }
        return handles;
    }
}